package com.design03_behavioral.pattern19_visitor.example01;

import java.text.NumberFormat;
import java.time.LocalDate;

/**
 * @ClassName DiscountVisitor
 * @Description TODO
 * @Author 22963
 * @DATE 2023/9/10 11:10
 */
public class DiscountVisitor implements Visitor {

    /*
    * NumberFormat
    * NumberFormat nf = NumberFormat.getInstance();  // 获取当前默认语言环境的NumberFormat实例
    * double number = 1234567.89;
    * String formattedNumber = nf.format(number);
    * System.out.println(formattedNumber);  // 输出: 1,234,567.89
    * */

    private LocalDate billDate;

    public DiscountVisitor(LocalDate billDate) {
        this.billDate = billDate;
        System.out.println("结算日期: " + billDate);
    }

    @Override
    public void visit(Candy candy) {
        System.out.println("糖果: " + candy.getName());
        //获取产品生产天数
        long days = billDate.toEpochDay() - candy.getProducedDate().toEpochDay();
        if (days > 180) {
            System.out.println("超过半年的糖果,请勿食用!");
        } else {
            double rate = 0.9;
            double discountPrice = candy.getPrice() * rate;
            System.out.println("糖果打折后的价格" + NumberFormat.getCurrencyInstance().format(discountPrice));
        }
    }

    @Override
    public void visit(Wine wine) {
        System.out.println("酒类: " + wine.getName() + ",无折扣价 格!");
        System.out.println("原价: " + NumberFormat.getCurrencyInstance().format(wine.getPrice()))
        ;
    }

    @Override
    public void visit(Fruit fruit) {
        System.out.println("水果: " + fruit.getName());
        //获取产品生产天数
        long days = billDate.toEpochDay() -
                fruit.getProducedDate().toEpochDay();
        double rate = 0;
        if (days > 7) {
            System.out.println("超过七天的水果,请勿食用!");
        } else if (days > 3) {
            rate = 0.5;
        } else {
            rate = 1;
        }

        double discountPrice = fruit.getPrice() * fruit.getWeight() * rate;
        System.out.println("水果价格: " + NumberFormat.getCurrencyInstance().format(discountPrice));
    }

    @Override
    public void visit(com.design03_behavioral.pattern19_visitor.example02.Candy candy) {

    }

    @Override
    public void visit(com.design03_behavioral.pattern19_visitor.example02.Wine wine) {

    }

    @Override
    public void visit(com.design03_behavioral.pattern19_visitor.example02.Fruit fruit) {

    }

    public static void main(String[] args) {
        LocalDate billDate = LocalDate.now();
        Candy candy = new Candy("徐福 记", LocalDate.of(2022, 10, 1), 10.0);
        System.out.println("糖果: " + candy.getName());
        double rate = 0.0;
        long days = billDate.toEpochDay() - candy.getProducedDate().toEpochDay();
        System.out.println(days);
        if (days > 180) {
            System.out.println("超过半年的糖果,请勿食用!");
        } else {
            rate = 0.9;
            double discountPrice = candy.getPrice() * rate;
            System.out.println("打折后的价 格" + NumberFormat.getCurrencyInstance().format(discountPrice));
        }
    }

}
